package com.hexinfo.dmpro.sparing.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.hexinfo.dmpro.sparing.async.AsyncTableTask;
import com.hexinfo.dmpro.sparing.model.HotStandbyLogSource;
import com.hexinfo.dmpro.sparing.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FailTableRetryServiceImpl implements FailTableRetryService {

    private final HotStandbyLogSourceService hotStandbyLogSourceService;
    private final AsyncTableTask asyncTableTask;
    private final FailTableRetrySourceService failTableRetrySourceService;

    /**
     * 源级热备、表级热备，失败表重新热备（自动失败重跑）
     * @param sourceName 源名
     */
    @Override
    public Boolean failTableRetrySource(String sourceName){
        //通过源名，获取最近的源热备任务
        HotStandbyLogSource hotStandbyLogSource = hotStandbyLogSourceService.querySourceName(sourceName);
        if (ObjectUtil.isEmpty(hotStandbyLogSource)){
            log.error(sourceName+"没有热备任务，无法进行热备失败重跑！！！！！！！！！！！！！！");
            return false;
        }
        //大表同步（只保留两年内日期分区数据）
        asyncTableTask.failTableRetry(sourceName,hotStandbyLogSource.getTaskDate());
        //源级
        return  failTableRetrySourceService.failTableRetrySource(sourceName,hotStandbyLogSource);
    }

}
